<?php

/*
 * Tomato Framework
 * 
 * Copyright 2012-2012 Luax.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @link http://www.tomatoframework.com
 * @link http://www.luaxlou.com
 * @version $Id$
 */

/**
 * 
 *
 * @author Luax <luaxcn@gmail.com>
 */
class Rest {

    public static $defaultResource = 'default';
    public static $defaultAction = 'list';
    public static $defaultType = 'html';
    
 
    
    public static $patterns = array(
        '!^/(\w+)/(show|edit|save|delete|update)/(\w+)\.(json|xml|html)!',
        '!^/(\w+)/(show|edit|save|delete|update)/(\w+)()!',
        '!^/(\w+)/(|list|create|save|delete|update)()\.(json|xml|html)!',
        '!^/(\w+)/(list|create|save|delete|update)()()!',
        '!^/(\w+)()()()!',
    );
    public static $vars = array(
        'URI',
        'resource',
        'action',
        'id',
        'type'
    );

 
    
    public static function get($requestURI = null) {
        if ($requestURI == null) {
            $requestURI = $_SERVER['REQUEST_URI'];
        }

        $rest = array_fill_keys(Rest::$vars, null);

        $matches = array();
        foreach (Rest::$patterns as $pattern) {
            if (preg_match($pattern, $requestURI, $matches)) {

                $rest = array_combine(Rest::$vars, $matches);

                break;
            }
        }


        if ($rest['resource'] == null) {
            $rest['resource'] = Rest::$defaultResource;
        }
        if ($rest['action'] == null) {
            $rest['action'] = Rest::$defaultAction;
        }
        if ($rest['type'] == null) {
            $rest['type'] = Rest::$defaultType;
        }

        return $rest;
    }

    public static function create($resource, $action, $id) {
        //@todo write code here
    }

}

?>
